<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        :class="{active:activeIndex === index}"
        class="role-item"
        @click="activeFn(index,item.roleId)"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
              v-auth-dir="`sys:role:add_edit`"
              @click.native="$router.push(`/add-role?roleId=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item
              v-auth-dir="`sys:role:remove`"
              style="color: red;" @click.native="delRoleFn(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button v-auth-dir="`sys:role:add_edit`" class="addBtn" size="mini" @click="$router.push('/add-role')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in TreeData" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="myTree"
                :data="item.children"
                node-key="id"
                :props="{label :'title',disabled:'flag'}"
                show-checkbox
                default-expand-all
                :expand-on-click-node="false"
                check-on-click-node
                check-strictly
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员 ${total}`" name="member">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getRoleTreeAPI, getRoleDetailAPI, getRoleUserAPI, delRoleAPI } from '@/apis/role'
export default {
  name: 'Role',
  data() {
    return {
      TreeData: [],
      props: {
        label: 'title',
        children: 'childran'
      },
      count: 1,
      roleList: [], // 获取角色列表
      activeIndex: 0, // 记录哪行高亮
      roleDetailList: [],
      activeName: 'tree',
      roleUserList: [], // 角色关联的用户列表
      total: 0 // 角色关联的用户总数
    }
  },
  created() {
    this.getList()
    this.getTree()
  },
  methods: {
    // 删除角色
    async delRoleFn(roleId) {
      const res = await delRoleAPI(roleId)
      await this.getList()
      if (res.code === 10000) {
        this.$message({
          type: 'success',
          message: '角色已移除'
        })
      }
    },
    // 获取角色关联的用户信息
    async getRoleUserList(id) {
      const res = await getRoleUserAPI(id)
      console.log(res)
      this.roleUserList = res.data.rows
      this.total = res.total
    },
    // 获取指定用户的权限树
    async getRoleDetail(id) {
      const res = await getRoleDetailAPI(id)
      // console.log(res)
      // this.roleDetailList = res.data.perms
      this.$refs.myTree.forEach((components, index) => {
        components.setCheckedKeys(res.data.perms[index])
      })
    },
    setFlagFn(obj) {
      obj.flag = true
      if (obj.children) {
        obj.children.forEach(smllobj => {
          this.setFlagFn(smllobj)
        })
      }
    },
    // 获取权限树
    async getTree() {
      const res = await getRoleTreeAPI()
      // console.log(res)
      // this.loadNode(res.data,res.title)
      res.data.forEach(item => {
        this.setFlagFn(item)
      })
      this.TreeData = res.data
    },
    // 点击高亮
    activeFn(index, id) {
      this.activeIndex = index
      this.getRoleDetail(id)
      this.getRoleUserList(id)
    },
    // 获取角色列表
    async getList() {
      const res = await getRoleListAPI()
      console.log(res)
      this.roleList = res.data
      this.getRoleDetail(res.data[0].roleId)
      this.getRoleUserList(res.data[0].roleId)
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
